package com.mongodb.mongodbboot.service.impl;

import com.mongodb.client.MongoCursor;
import com.mongodb.mongodbboot.config.MongoShardingConfig;
import com.mongodb.mongodbboot.service.SampleQueryService;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author huyunqiang
 * @Description
 * @Date 2025/5/10 13:28
 **/
@Service
public class SampleQueryServiceImpl implements SampleQueryService {

    @Autowired
    private MongoShardingConfig mongoShardingConfig;

    @Override
    public List<Document> query(String tableName, Bson filter) {

        List<Document> result = new ArrayList<>();
        List<Document> data = new ArrayList<>();
        MongoCursor<Document> cursor = mongoShardingConfig.getHistoryMongoTemplate().getMongoDbFactory().getMongoDatabase("AMDB").getCollection(tableName).find(filter).batchSize(10000)
                .iterator();
        while (cursor.hasNext()) {
            data.add(cursor.next());
        }
        result.addAll(data);
        return result;
    }
}
